package com.huadi.cmcc.controller;

import com.huadi.cmcc.domain.Admin;
import com.huadi.cmcc.domain.Result;
import com.huadi.cmcc.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author zhw
 * @date 2021/8/31
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public Result<Admin> login(String username, String password, HttpSession session) {
        Admin admin = adminService.login(username, password);
        if (admin == null) {
            return Result.fail("用户名或密码错误");
        }
        admin.setPassword(null);
        session.setAttribute("admin", admin);
        return Result.success(admin);
    }

    @PostMapping("/logout")
    public Result logout(HttpSession session) {
        session.removeAttribute("admin");
        return Result.success();
    }
}
